//
//  QCEmotionKeyboardView.swift
//  SwiftDemoWithSina
//
//  Created by qu on 2017/2/10.
//  Copyright © 2017年 Apple. All rights reserved.
//

import UIKit

class QCEmotionKeyboardView: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func setupUI(){
        self.frame.size = CGSize(width: KSCREENW, height: 216)
        backgroundColor = UIColor(patternImage: UIImage(named: "emoticon_keyboard_background")!)
        
        addSubview(emotionBottomView)
        addSubview(emotionPageView)
        addSubview(pageControl)
        emotionBottomView.snp.makeConstraints { (make) in
            make.left.bottom.right.equalToSuperview()
            make.height.equalTo(35)
        }
        
        emotionPageView.snp.makeConstraints { (make) in
            make.left.top.right.equalToSuperview()
            make.bottom.equalTo(emotionBottomView.snp.top)
        }
        
        pageControl.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.bottom.equalTo(emotionPageView).offset(10)
        }
        
        //当前滚动pageView 时 界面还没有加载完成 所以无法看到效果 所以需要GCD
        DispatchQueue.main.async { 
            //设置默认滚到第一组
            let  indexPath = IndexPath(item: 0, section: 1)
            //让pageView滚动到指定的组
            self.emotionPageView.scrollToItem(at: indexPath, at: UICollectionViewScrollPosition.left, animated: false)
            //设置pageControl
            self.setupPageControl(indexPath: indexPath)
            
        }
        
        emotionBottomView.cloure = {(type)->() in
            switch type {
            case .recent:
                print("最近")
            case .normal:
                print("默认")
            case .emoji:
                print("emoji")
            case .lxh:
                print("浪小花")
                
            }
            
          let indexPath = IndexPath(item: 0, section: type.rawValue - 100)
            self.emotionPageView.scrollToItem(at: indexPath, at: UICollectionViewScrollPosition.left, animated: false)
            self.setupPageControl(indexPath: indexPath)
        }
    }
    
    fileprivate lazy var emotionBottomView:QCEmotionBottomView = QCEmotionBottomView()
    fileprivate lazy var emotionPageView:QCEmotionPageView = {
        
        let view = QCEmotionPageView()
        
        //设置代理
        view.delegate = self
        // 取消弹簧效果
        view.bounces = false
        // 设置分页
        view.isPagingEnabled = true
        // 取消滚动条
        view.showsVerticalScrollIndicator = false
        view.showsHorizontalScrollIndicator = false
        
        view.backgroundColor = self.backgroundColor
        
        return view
    }()
    
    fileprivate lazy var pageControl: UIPageControl = {
        
        let view = UIPageControl()
        
        view.numberOfPages = 4
        
        view.currentPage = 1
        
        //设置默认图片
      view.setValue(UIImage(named:"compose_keyboard_dot_normal"), forKey: "pageImage")
        
    view.setValue(UIImage(named:"compose_keyboard_dot_selected"), forKey: "currentPageImage")
        
        view.isUserInteractionEnabled = false
        // 总页数为1的时候不显示
        view.hidesForSinglePage = true
        
        return view
    }()
}
///代理
extension QCEmotionKeyboardView: UICollectionViewDelegate{
    //监听当前在屏幕中心的indexPath
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        
        //获取中心点
        let centerX = KSCREENW/2 + scrollView.contentOffset.x
        
        //获取y
        let centerY = CGFloat((216 - 35)/2)
        
        let center = CGPoint(x: centerX, y: centerY)
        
        if let indexPath = self.emotionPageView.indexPathForItem(at: center){
        //设置选中的按钮状态
            
            self.emotionBottomView.setupSelectButton(tag: indexPath.section + 100)
        //设置pageControl
        self.setupPageControl(indexPath: indexPath)
            
        }
    }
}

extension QCEmotionKeyboardView{
    //设置pageControl
    func setupPageControl(indexPath: IndexPath){
        
        self.pageControl.numberOfPages = QCEmotionTools.shared.allEmotions[indexPath.section].count
        self.pageControl.currentPage = indexPath.item
        
    }
}
